from forward.api.admin.schemas.channel import ChannelH5InSchema
from forward.utils.config import ConfigUtil
from forward.utils.qr_code import QRCodeUtils


class ChannelH5Service:
    """H5渠道设置服务实现类"""

    async def detail(self) -> dict:
        """H5渠道配置详情"""
        config = await ConfigUtil.get("h5_channel")
        config["status"] = int(config.get("status", 0))
        config["close"] = int(config.get("close", 0))
        config["url"] = config.get("url", "")
        config["domain"] = config.get("domain", "")
        return config

    async def save(self, h5_in: ChannelH5InSchema):
        """H5渠道配置保存"""
        await ConfigUtil.set("h5_channel", "status", str(h5_in.status))
        await ConfigUtil.set("h5_channel", "close", str(h5_in.close))
        await ConfigUtil.set("h5_channel", "url", h5_in.url)
        await ConfigUtil.set("h5_channel", "domain", h5_in.domain)
        await ConfigUtil.set("h5_channel", "domainQrcode", await QRCodeUtils.create_qr_code(h5_in.domain, "h5DomainQrcode"))

    @classmethod
    async def instance(cls):
        """实例化"""
        return cls()
